# -*- coding:utf-8 -*-

import configparser
import xlsxwriter
import copy
import os
import time
from datetime import datetime, timedelta


class create_info_excel(object):
    def __init__(self):
        pass

    def generate_info_excel(self,title, exceldata):
        try:
            os.remove(r"file/info.xlsx")
        except:
            pass
        ##创建xlsx状态
        workbook = xlsxwriter.Workbook(r"file/info.xlsx")
        worksheet = workbook.add_worksheet()

        ##格式配置
        worksheet.set_column('A:F', 20)
        # 标题
        format_title = workbook.add_format({'font_size': 20, 'bold': True, 'border': 1})
        format_title.set_align('center')
        format_title.set_align('vcenter')

        # 第一行
        format_h1 = workbook.add_format({'font_size': 12, 'bold': True, 'border': 1})
        format_h1.set_align('center')
        format_h1.set_align('vcenter')

        # 第二行
        format_h2 = workbook.add_format(
            {'font_size': 12, 'bold': True, 'border': 1, 'font_color': '#FF0000', 'bg_color': '#FFFF00'})
        format_h2.set_align('center')
        format_h2.set_align('vcenter')

        # 内容
        format_data = workbook.add_format({'font_size': 10, 'border': 1})
        format_data.set_align('center')
        format_data.set_align('vcenter')

        # 标题二
        format_title_2 = workbook.add_format(
            {'font_size': 12, 'bold': True, 'border': 1, 'font_color': '#FF0000', 'bg_color': '#FFFF00'})
        format_title_2.set_align('center')
        format_title_2.set_align('vcenter')

        # 整合数据
        h2_data1 = ['标题','类型','标签','频道','点击量','创建时间']

        # 创建表格
        worksheet.merge_range('A1:F1', title, format_title)
        worksheet.write_row('A2', h2_data1, format_h2)
        n = 3
        for cae_i in exceldata:
            excel_one = []
            for k, v in cae_i.items():
                excel_one.append(v)
            worksheet.write_row('A%s' % (n), excel_one, format_data)
            n+=1
        chart1 = workbook.add_chart({'type': 'column'})

        workbook.close()
        print("excel已经生成")